package com.vids.service.impl;

import com.vids.utils.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vids.model.entity.*;
import com.vids.model.param.*;
import com.vids.model.vo.ImageVo;
import com.vids.model.vo.SubImageInfoVo;
import com.vids.mapper.*;
import com.vids.service.IImageBusinessService;
import com.vids.service.IImageService;
import com.vids.utils.BeanUtilss;
import com.vids.utils.QueryUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 图片
 * </p>
 *
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ImageServiceImpl extends ServiceImpl<ImageMapper, ImageInfo>
	implements IImageService {
    private final MetaTypeMapper metaTypeMapper;
    private final ImageMapper imageMapper;

    private final FaceMapper faceMapper;
    
    private final MotorVehicleMapper motorVehicleMapper;
    
    private final IImageBusinessService tBigDataImageBusinessServiceImpl;

    @Override
    public ImageInfo findByImageID(String ImageID) {
	return baseMapper.findByImageID(ImageID);
    }

    @Override
    public ImageVo findDataByImageID(String ImageID) {
	return baseMapper.findDataByImageID(ImageID);
    }

    @Override
    public ImageBatchParam getImageList(Map<String, Object> map) {
	// 无条件查询默认返回所有
	if (map == null || map.size() == 0) {
	    List<ImageInfo> resultList = imageMapper.findAll();
	    return buildResultModel(resultList);
	}
	Map<String, Object> paramMap = QueryUtils.buildParam(map);
	List<ImageInfo> resultList = imageMapper.getImageList(paramMap);
	return buildResultModel(resultList);
    }

    private ImageBatchParam buildResultModel(List<ImageInfo> resultList) {
	if (resultList != null && resultList.size() > 0) {
	    ImageBatchParam param = new ImageBatchParam();
	    ImageBatchModel batchModel = new ImageBatchModel();
	    List<ImageModel> modelList = new ArrayList<>();
	    for (ImageInfo image : resultList) {
		ImageModel model = new ImageModel();
		model.setImageInfo(image);
		setImageRelaMeta(image.getImageID(), model);
		modelList.add(model);
	    }
	    batchModel.setImage(modelList);
	    param.setImageListObject(batchModel);
	    return param;
	}
	return null;
    }

    public void setImageRelaMeta(String id, ImageModel imageModel) {
	MetaType metaType = metaTypeMapper.getByTypeAndMetaID(2, id);
	if (metaType != null) {
	    if (!StringUtils.isEmpty(metaType.getCarIds())) {
		String[] arr = metaType.getCarIds().split(",");
		List<MotorVehicleModel> metaList = new ArrayList<>();
		for (String str : arr) {
		    MotorVehicle car = motorVehicleMapper.findByMotorVehicleID(str);
		    if (car != null) {
			MotorVehicleModel carModel = BeanUtilss.transferObjectCommon(car, MotorVehicleModel.class);
			List<SubImageInfoVo> volist = tBigDataImageBusinessServiceImpl
				.findSubImageInfoVo(car.getMotorVehicleID(), "01");
			if (volist != null && volist.size() > 0) {
			    SubImageBatchParam subImageList = new SubImageBatchParam();
			    subImageList.setSubImageInfoObject(volist);
			    carModel.setSubImageList(subImageList);
			}
			metaList.add(carModel);
		    }
		}
		if (metaList.size() > 0) {
		    MotorVehicleBatchModel batchModel = new MotorVehicleBatchModel();
		    batchModel.setMotorVehicleObject(metaList);
		    imageModel.setMotorVehicleList(batchModel);
		}
	    }
	    if (!StringUtils.isEmpty(metaType.getFaceIds())) {
		String[] arr = metaType.getFaceIds().split(",");
		List<FaceModel> metaList = new ArrayList<>();
		for (String str : arr) {
		    Face face = faceMapper.findByFaceID(str);
		    if (face != null) {
			FaceModel metaModel = BeanUtilss.transferObjectCommon(face, FaceModel.class);
			List<SubImageInfoVo> volist = tBigDataImageBusinessServiceImpl
				.findSubImageInfoVo(face.getFaceID(), "11");
			if (volist != null && volist.size() > 0) {
			    SubImageBatchParam subImageList = new SubImageBatchParam();
			    subImageList.setSubImageInfoObject(volist);
			    metaModel.setSubImageList(subImageList);
			}
			metaList.add(metaModel);
		    }
		}
		if (metaList.size() > 0) {
		    FaceBatchModel batchModel = new FaceBatchModel();
		    batchModel.setFaceObject(metaList);
		    imageModel.setFaceList(batchModel);
		}
	    }


	}
    }
}
